# 函数get() return 最新字典
# 函数check_new(字典类型) return (list)[最新字典，更新的信息字典]

import urllib.request
import re
def get():
    web = urllib.request.urlopen('http://jwc.shmtu.edu.cn/')
    html = web.read()
    html = html.decode('utf-8','ignore')
    each_lines = html.splitlines()
    line_num = 0 #当前行数
    time = 1
    information_dic = {}
    while(each_lines[line_num] != each_lines[-1]):
        if each_lines[line_num] == '          <tr class="odd">' or each_lines[line_num] == '          <tr class="even">' :
            information = each_lines[line_num + 2]
            url = 'http://jwc.shmtu.edu.cn/'
            urls = re.findall('href=".*">',information)
            names = re.findall('"behindtitle">.*</div>',information)
            url += urls[0][7:-27]
            name = names[0][14:-6]
            information_dic[name] = url
            if re.findall('                  <td class="text-nowrap pubdate" >',each_lines[line_num+5]) and ('          <tr class="odd">' or each_lines[line_num] == '          <tr class="even">') and time:
                latest_time = each_lines[line_num+6][12:22]
                time = 0
                information_dic['latest_time'] = latest_time
        line_num += 1
    return information_dic

def check_new(information_dic):
        latest_dic = get()
        update_dic = {}
        if latest_dic == information_dic:
            print("教务处网站无更新")
        else:
            print("教务处网站有更新！最新公告日期为",latest_dic['latest_time'],"正在获取最新数据。。。")
            for each_key in latest_dic:
                if each_key not in information_dic:
                    update_dic[each_key] = latest_dic[each_key]
        return [latest_dic,update_dic]

if __name__ == "__main__":
    dic = get()
    print(check_new(dic))